package com.briup.corejava.day19;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 从a.txt中读取内容，写入到一个字节数组中
 */
public class Test1 {
    public static void main(String[] args) {
        // 1、选择流：文件字节输入流，字节数组输出流
        // 2、声明流
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        // 3、创建流
        try {
            fis = new FileInputStream("src/com/briup/corejava/day18/file/a.txt");
            baos = new ByteArrayOutputStream();
            // 边读边写
            byte[] b = new byte[1024];
            int len = -1;
            while((len = fis.read(b))!=-1){
                baos.write(b,0,len);
            }
            // 从输出流中获取数据数组
            byte[] byteArray = baos.toByteArray();
            System.out.println(new String(byteArray));
            System.out.println("读写完毕");

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(baos !=null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
